// components/popup/payment/index.js
import User from '../../../utils/user';
import util from "../../../utils/utils";
import { CartHttp } from '../../../api/cart.js'
const cartModel = new CartHttp();

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    order_id: {
      type: String,
      value: ''
    },
    payMethod: {
      type: Boolean,
      value: false
    },
    show: {
      type: Boolean,
      value: false
    },
    payment: {
      type: Number,
      value: 4
    }, // 支付方式
  },
  isRequestPay: false,
  /**
   * 组件的初始数据
   */
  data: {
    pay_key:'',
    pwdBox:false,
    userInfo:User.UserData,
    showPayTime:false,
    handleType:1,
    checkedIcon: {
      active: '/images/icon/icon_check_active.png',
      normal: '/images/icon/icon_check_inactive.png',
      normalEmpty: '/images/icon/icon_check_inactive_empty.png'
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 对外调用的方法
     * 打开支付方式选择器
     */
    openPayMethod(){
      this.setData({
        payMethod:true
      });
    },
    startPayBox(){
      if(util.fixInt(this.data.payment) === 3){
        this.requestPay();
      }else{
        if(User['UserData']['common']['pass_noneed'] === true){
          this.requestPay();
        }else{
          this.setData({
            pwdInput:true,
          });
          let com = this.selectComponent('#pwdBox');
          com.getFocus();
          if(this.data.showPayTime === true){
            com.countdown();
          }
        }
      }
    },
    switchPayment(payment){
      this.setData({
        payment: util.fixInt(payment),
      });
    },
    setOrderID(order_id){
      this.setData({
        order_id: order_id,
      });
    },
    setShowPayTime(flag){
      this.setData({
        showPayTime: flag,
      });
    },
    setHandleType(flag){
      this.setData({
        handleType: flag,
      });
    },
    requestPay:function(pay_key){
      let self = this;
      let param = {
        union_order_id:this.data.order_id,
        pay_type:this.data.payment,
        pay_key:pay_key,
        os:0
      };
      if(self.isRequestPay === true){
        return false;
      }
      self.isRequestPay = true;
      util.request({
        url:'s/wxpay/BeginGoodsPay',
        data:param,
        loading:true,
        handle:true,
        error:function(){
          self.isRequestPay = false;
        },
        callback:function (result) {
          self.isRequestPay = false;
          if(result.status === 'ok'){
            if(self.data.payment === 3){
              let param = {
                timeStamp:result.res['mini']['timeStamp'],
                nonceStr:result.res['mini']['nonceStr'],
                signType:result.res['mini']['signType'],
                package:result.res['mini']['package'],
                paySign:result.res['mini']['paySign'],
              };
              param.success = function(result){
                util.msg('支付成功！');
                self.triggerEvent('complete', {
                  status:'complete'
                });
              };
              param.fail = function(res){
                if(res.errMsg === 'requestPayment:fail cancel'){
                  util.msg('您取消了支付');
                }
                self.triggerEvent('payError', {
                  status:'error'
                });
              };
              wx.requestPayment(param);
            }else{
              if(result.status === 'ok'){
                util.msg('支付成功');
                self.triggerEvent('complete', {
                  status:'complete'
                });
              }else{
                util.msg('支付失败：' + result['msg']);
                self.triggerEvent('payError', {
                  status:'error'
                });
              }
            }
          }else{
            util.msg('支付错误：' + result['msg']);
            self.triggerEvent('payError', {
              status:'error'
            });
          }
        }
      });
    },
    onConfirmPwdInput(e) {
      let pay_key = '';
      if(e.detail.hasOwnProperty('pay_key')){
        pay_key = e.detail.pay_key;
      }
      if(this.data.handleType === 1){
        this.requestPay(pay_key);
      }else{
        this.triggerEvent('confirmPwd', {
          pay_key: e.detail.pay_key
        });
      }
    },
    // 支付方式弹出层 - 关闭
    onClosePayment() {
      this.setData({ payMethod: false })
    },
    // 支付方式弹出层 - 确认
    onConfirmPayment(event) {
      let payment = util.fixInt(event.detail.payment);
      this.setData({
        payMethod: false,
        payment: payment,
      });
      this.startPayBox();
    },
    // 关闭
    onClose () {
      this.triggerEvent('close')
    },
    // 选择支付方式
    onSelectPayment (event) {
      const { name } = event.currentTarget.dataset;
      this.setData({ payment: name });
    },
    // 确认
    onConfirm () {
      this.triggerEvent('confirm', {
        payment: this.data.payment
      }, {});
    }
  }
});
